﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv="Page-Enter" content="revealTrans(duration=0.5, transition=8)" />
    <script type="text/javascript" src="../Scripts/QueryString.js"></script>
    <script type="text/javascript" src="../Scripts/config.js"></script>
    <script src="Gener.js" type="text/javascript"></script>

    <script src="../Scripts/easyUI/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script src="../Scripts/easyUI/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../Scripts/EasyUIUtility.js" type="text/javascript"></script>
    <script src="./JS/Calendar/WdatePicker.js" defer="defer" type="text/javascript"></script>
    <link href="../Scripts/easyUI/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../Scripts/easyUI/themes/icon.css" rel="stylesheet" type="text/css" />
    <link href="../../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript">

        var newRowIdx = -1;
        var frmData;
        var mapData;
        var dtM; //方法集合.
        var webUser = new WebUser();
        var mapAtrrs;
        var InsertFlag = 0;
        var PK;//实体的主键

        $(function () {
            InitPage();

        });

        function InitPage() {
            InsertFlag = 0;
            var httpHandler = new HttpHandler("BP.WF.HttpHandler.WF_Comm");
            httpHandler.AddUrlData();  //增加参数属性.

            var data = httpHandler.DoMethodReturnString("Entitys_Init");
            if (data.indexOf('err@') == 0) {
                $("#Msg").html(data);
                return;
            }

            //解析json.
            frmData = JSON.parse(data);
            console.log(frmData);

            mapData = frmData["Sys_MapData"][0];
            mapData = new Entity("BP.Sys.MapData", mapData); //把他转化成entity.

            document.title = mapData.Name;

            //toolBar
            if (mapData.GetPara("IsInsert") == "1")
                $("#newbtn").show();
            else
                $("#newbtn").hide();

            if (mapData.GetPara("IsDelete") == "1")
                $("#delbtn").show();
            else
                $("#delbtn").hide();
            PK = mapData.GetPara("PK");
            //绑定数据.
            BindDtls();
        }

        //绑定明细.
        function BindDtls() {
           
            mapAtrrs = frmData.Sys_MapAttr;
            var dtls = frmData.Ens;

            var html = "<table border=0 cellpadding='0'  style='border-collapse: collapse;width:100%' cellspacing='0' >";
            html += "<tr>";
            html += "<th class='GroupTitle' nowrap=''style='width:70px;text-align:center'><input id='checkedAll' onclick='SelectAll(this)' type='checkbox' name='checkedAll'></th>";
           

            for (var i = 0; i < mapAtrrs.length; i++) {
                var mapAttr = mapAtrrs[i];
                mapAttr = new Entity("BP.Sys.SysMapAttr", mapAttr);
                if (mapAttr.UIVisible == "0")
                    continue;
                html += "<th class='GroupTitle' nowrap=''>" + mapAttr.Name + "</th>";
            }
            html += "<th class='GroupTitle' nowrap=''>....</th>";
            html += "</tr>";


            var hidden = "";

            //生成内容.
            for (var i = 0; i < dtls.length; i++) {

                var dtl = dtls[i];
                dtl = new Entity("BP.Sys.GEDtl", dtl);
                var pkval = dtl.No;
                if (pkval == undefined) {
                    pkval = dtl.OID;
                }
                if (pkval == undefined) {
                    pkval = dtl.MyPK;
                }
                if (pkval == undefined) {
                    pkval = dtl.WorkID;
                }
                if (pkval == undefined) {
                    pkval = dtl.NodeID;
                }

                html += "<tr>";
                html += "<td class='Idx' nowrap style='width:70px'><input id='IDX_" + pkval + "'  type='checkbox' name='IDX_" + pkval + "' /><label for='IDX_" + pkval + "'>" + (i + 1) + "</label></td>";
              

                for (var attrIdx = 0; attrIdx < mapAtrrs.length; attrIdx++) {
                    var mapAttr = mapAtrrs[attrIdx];
                    mapAttr = new Entity("BP.Sys.SysMapAttr", mapAttr);
                    if (mapAttr.UIVisible == "0") {
                        html += "<input type='hidden' id='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' value='" + dtl[mapAttr.KeyOfEn] + "' />";
                        continue;
                    }
                    html += "<td>" + InitMapAttrOfCtrl(mapAttr, dtl, pkval) + "</td>";
                }
                html += "<td style='width:40px'><a href=\"javascript:OpenEn('" + pkval + "')\">详细</a></td>";

                html += "</tr>";
            }

            html += "</table>";
            var html = $(html);
            $('#mytable').html("").append(html);
            $('#mytable').append(hidden);
         
            
        }


        function InitMapAttrOfCtrl(mapAttr, dtl, pkval) {

            var str = '';
            var defValue = ConvertDefVal(mapAttr, dtl);
            var isInOneRow = false; //是否占一整行
            var islabelIsInEle = false; //
            var eleHtml = '';

            //外部数据源类型.
            if (mapAttr.LGType == "0" && mapAttr.MyDataType == "1" && mapAttr.UIContralType == "1") {

                //枚举类型.
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = "";
                else
                   enableAttr = "disabled='disabled'";

                return "<select id='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' name='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' " + (mapAttr.UIIsEnable == 1 ? '' : 'disabled="disabled"') + ">" + InitDDLOperation(frmData, mapAttr, defValue) + "</select>";
            }

            //外键类型.
            if (mapAttr.LGType == "2" && mapAttr.MyDataType == "1") {

                //枚举类型.
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = "";
                else
                    enableAttr = "disabled='disabled'";

                return "<select id='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "'  name='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' " + (mapAttr.UIIsEnable == 1 ? '' : 'disabled="disabled"') + ">" + InitDDLOperation(frmData, mapAttr, defValue) + "</select>";
            }

            //外部数据类型.
            if (mapAttr.LGType == "0" && mapAttr.MyDataType == "1" && mapAttr.UIContral == 1) {

                //枚举类型.
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = "";
                else
                    enableAttr = "disabled='disabled'";

                return "<select id='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' name='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' " + (mapAttr.UIIsEnable == 1 ? '' : 'disabled="disabled"') + ">" + InitDDLOperation(frmData, mapAttr, defValue) + "</select>";
            }

            //添加文本框 ，日期控件等
            //AppString
            if (mapAttr.MyDataType == "1" && mapAttr.UIContralType == 0) {  //不是外键

                //  alert(mapAttr.UIWidth);


                if (mapAttr.UIHeight <= 40) //普通的文本框.
                {
                    var enableAttr = '';
                    if (mapAttr.UIIsEnable == 0)
                       enableAttr = "disabled='disabled'";

                    return "<input  maxlength=" + mapAttr.MaxLen + "  name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' id='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' value='" + defValue + "'  type='text'  " + enableAttr + " />";
                }

                //普通的大块文本.
                return "<textarea maxlength=" + mapAttr.MaxLen + " name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' id='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' style='width:" + mapAttr.UIWidth + "px' type='text'  " + (mapAttr.UIIsEnable == 1 ? '' : ' disabled="disabled"') + " />"
            }

            //日期类型.
            if (mapAttr.MyDataType == 6) {
                var enableAttr = '';
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = 'onfocus="WdatePicker({dateFmt:' + "'yyyy-MM-dd'})" + '";';
                else
                   enableAttr = "disabled='disabled'";

                return "<input " + enableAttr + " value='" + defValue + "' id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "'  name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' type='text' />";
            }

            //时期时间类型.
            if (mapAttr.MyDataType == 7) {

                var enableAttr = '';
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = 'onfocus="WdatePicker({dateFmt:' + "'yyyy-MM-dd HH:mm'})" + '";';
                else
                   enableAttr = "disabled='disabled'";

                return "<input id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' type='text' value='" + defValue + "' style='width:120px;' " + enableAttr + "  name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' />";
            }

            // boolen 类型.
            if (mapAttr.MyDataType == 4) {  // AppBoolean = 7

                if (mapAttr.UIIsEnable == 1)
                    enableAttr = "";
                else
                   enableAttr = "disabled='disabled'";

                //CHECKBOX 默认值
                var checkedStr = '';
                if (checkedStr != "true" && checkedStr != '1') {
                    checkedStr = ' checked="checked" ';
                }

                checkedStr = "";

                return "<input id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' " + enableAttr + " value='" + defValue + "' " + (defValue == 1 ? "checked='checked'" : "") + " type='checkbox' id='CB_" + pkval + "_" + mapAttr.KeyOfEn + "'  name='CB_" + pkval + "_" + mapAttr.KeyOfEn + "' " + checkedStr + "  /><label for='CB_" + pkval + "_" + mapAttr.KeyOfEn + "' ></label>";
            }

            //枚举类型.
            if (mapAttr.MyDataType == 2 && mapAttr.LGType == 1) { //AppInt Enum
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = "";
                else
                   enableAttr = "disabled='disabled'";

                return "<select id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' name='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' " + (mapAttr.UIIsEnable == 1 ? '' : 'disabled="disabled"') + ">" + InitDDLOperation(frmData, mapAttr, defValue) + "</select>";
            }

            // AppDouble  AppFloat
            if (mapAttr.MyDataType == 5 || mapAttr.MyDataType == 3) {

                var enableAttr = '';
                if (mapAttr.UIIsEnable != 1)
                   enableAttr = "disabled='disabled'";

                return "<input id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' style='text-align:right;' value='" + defValue + "'  onkeyup=" + '"' + "if(isNaN(value)) execCommand('undo')" + '"' + " onafterpaste=" + '"' + "if(isNaN(value))execCommand('undo')" + '"' + " maxlength=" + mapAttr.MaxLen / 2 + "   type='text'" + enableAttr + " name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "'/>";
            }

            if ((mapAttr.MyDataType == 2)) { //AppInt
                var enableAttr = '';
                if (mapAttr.UIIsEnable == 0 || mapAttr.KeyOfEn == "NodeID") {
                    enableAttr = "disabled='disabled'";
                }
                return "<input  id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' style='text-align:right;' value='" + defValue + "' onkeyup=" + '"' + "if(isNaN(value) || (value%1 !== 0))execCommand('undo')" + '"' + " onafterpaste=" + '"' + "if(isNaN(value) || (value%1 !== 0))execCommand('undo')" + '"' + " maxlength=" + mapAttr.MaxLen / 2 + "   type='text'" + enableAttr + " name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "'/>";
            }

            //AppMoney  AppRate
            if (mapAttr.MyDataType == 8) {
                var enableAttr = '';
                if (mapAttr.UIIsEnable == 1) {

                } else {
                    enableAttr = "disabled='disabled'";
                }
                return "<input id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' style='text-align:right;' value='" + defValue + "' onkeyup=" + '"' + "if(isNaN(value))execCommand('undo')" + '"' + " onafterpaste=" + '"' + "if(isNaN(value))execCommand('undo')" + '"' + " maxlength=" + mapAttr.MaxLen / 2 + "   type='text'" + enableAttr + " name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "'/>";
            }

            alert(mapAttr.Name + "的类型没有判断.");
            return;
        }

        //填充默认数据
        function ConvertDefVal(attr, dtl) {

            //通过MAINTABLE返回的参数
            for (var ele in dtl) {
                if (attr.KeyOfEn == ele && dtl != '') {
                    result = dtl[ele];
                    break;
                }
            }

            if (result != undefined && typeof (result) == 'string') {
                
            }
            return result = unescape(result);
        }

        //初始化下拉列表框的OPERATION
        function InitDDLOperation(flowData, mapAttr, defVal) {

            var operations = '';
            //外键类型.
            if (mapAttr.LGType == 2) {

                var data = frmData[mapAttr.KeyOfEn];
                if (data == undefined)
                    data = frmData[mapAttr.UIBindKey];

                if (data == undefined) {
                    alert('没有获得约定的数据源..' + mapAttr.KeyOfEn + " " + mapAttr.UIBindKey);
                    return;
                }



                $.each(data, function (i, obj) {
                    operations += "<option " + (obj.No == defVal ? " selected='selected' " : "") + " value='" + obj.No + "'>" + obj.Name + "</option>";
                });
            }


            //枚举类型.
            if (mapAttr.LGType == 1) {

                var enums = frmData.Sys_Enum;
                enums = $.grep(enums, function (value) {
                    return value.EnumKey == mapAttr.UIBindKey;
                });

                $.each(enums, function (i, obj) {
                    operations += "<option " + (obj.IntKey == defVal ? " selected='selected' " : "") + " value='" + obj.IntKey + "'>" + obj.Lab + "</option>";
                });
            }


            //外部数据源类型 MyFlowGener.js.InitDDLOperation
            if (mapAttr.LGType == 0) {
                var fn;
                try {
                    if (mapAttr.UIBindKey) {
                        fn = eval(mapAttr.UIBindKey);
                    }
                } catch (e) {
                }
                if (typeof fn == "function") {
                    $.each(fn.call(), function (i, obj) {
                        operations += "<option " + (obj.No == defVal ? " selected='selected' " : "") + " value='" + obj.No + "'>" + obj.Name + "</option>";
                    });
                } else if (typeof CommonHandler == "function") {
                    CommonHandler.call("", mapAttr.UIBindKey, function (data) {
                        GenerBindDDL("DDL_" + mapAttr.KeyOfEn, data, "No", "Name");
                    })
                } else {
                    alert('没有获得约定的数据源..' + mapAttr.KeyOfEn + " " + mapAttr.UIBindKey);
                }
            }
            return operations;
        }

        //新建.
        function New() {
            var ensName = GetQueryString("EnsName");
            var enName = ensName.substring(0, ensName.length - 1);
            var httpHandler = new HttpHandler("BP.WF.HttpHandler.WF_CommEntity");
            httpHandler.AddPara("EnName", enName);
            var data = httpHandler.DoMethodReturnString("EntityOnly_Init");
            frmData = JSON.parse(data);
            var mapAtrrs = frmData.Sys_MapAttr;
            mainData = frmData.MainTable[0];
           

            var html = "";
            html += "<tr>";
            html += "<td class='Idx'><font color=red>*</font></td>";

            var hidden = "";

            for (var attrIdx = 0; attrIdx < mapAtrrs.length; attrIdx++) {
                var mapAttr = mapAtrrs[attrIdx];
                mapAttr = new Entity("BP.Sys.SysMapAttr", mapAttr);
                if (mapAttr.UIVisible == "0") {
                    html += "<input type='hidden' id='TB_0_" + mapAttr.KeyOfEn + "' name='TB_0_" + mapAttr.KeyOfEn + "' value='" + mainData[mapAttr.KeyOfEn] + "' />";
                    continue;
                }
                html += "<td>" + InitMapAttrOfCtrl(mapAttr, mainData, "0") + "</td>";
            }
            html += "<td></td>";
            html += "</tr>";

            $("#mytable tbody:last").append(html);
            $("#newbtn").linkbutton('disable');

            InsertFlag = 1;
        }

        function OpenEn(pkVal) {

            var ensName = GetQueryString("EnsName");
            var enName = ensName.substring(0, ensName.length - 1);

            var url = "En.htm?EnName=" + enName + "&PKVal=" + pkVal + "&RefVal=" + GetQueryString("RefVal") + "&RefKey=" + GetQueryString("RefKey");

            OpenEasyUiDialogExt(url, this.document.title + ' : 详细', 800, 450, true);
        }

        //更新.
        function Save() {
            var handler = new HttpHandler("BP.WF.HttpHandler.WF_Comm");
            handler.AddUrlData();  //增加参数属性.
            formData = $("form").serialize();

            handler.AddFormData();
            handler.AddPara("InsertFlag", InsertFlag);
            //name = encodeURIComponent(name);
            var data = handler.DoMethodReturnString("Entities_Save");
            if (data.indexOf("err@") == 0) {
                alert(data);
                return;
            }
            $("#newbtn").linkbutton('enable');
            InitPage(); //刷新.
        }

        function Delete() {
            var ensName = GetQueryString("EnsName");
            if (window.confirm('您确定要删除吗？') == false)
                return;
           
            //获得存储的值.
            var cbs = $("input[type='checkbox']:checked");
            var str = "";
            
            cbs.each(function () {
                if (this.id.indexOf('IDX_') == -1)
                    return true;
                str = this.id.substring(4);
                var en = new Entity(ensName.substring(0, ensName.length - 1));
                en.No = str;
                en.MyPK = str;
                en.WorkID = str;
                en.NodeID = str;
                en.Delete();

            });

            InitPage(); //刷新.
          }

          function SelectAll(obj) {
              var checkeds = $("input[type='checkbox']");
              if (obj.checked == true) {
                  //获取name=box的复选框 遍历输出复选框
                  for (var i = 0; i < checkeds.length; i++) {
                      checkeds[i].checked = true;
                  }
              } else {
                  for (var i = 0; i < checkeds.length; i++) {
                      checkeds[i].checked = false;
                  }
              }

          }
    
 
    </script>
</head>
<body>
<form id="divCCForm"  method="post">
     <div data-options="region:'center'" style="padding: 0px;" border="false" style="overflow: hidden;">
         <div class="easyui-panel" >
             <div id="Btns" class="ToolBar" style="width:auto">
              
                <a href="#" class="easyui-linkbutton l-btn l-btn-plain" data-options="plain:true,iconCls:'icon-save'" onclick="Save()">保存</a>
                <a id="newbtn" href="#" class="easyui-linkbutton l-btn l-btn-plain" data-options="plain:true,iconCls:'icon-new'" onclick="New()">新增</a>
                <a id="delbtn" href="#" class="easyui-linkbutton l-btn l-btn-plain" data-options="plain:true,iconCls:'icon-delete'" onclick="Delete()">删除</a>
                 </div>
              <div id="mytable"> </div>
         </div>
      </div> 
                
    <div id="Msg"></div>              
</form>
     
</body>

</html>
